<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.code" placeholder="请选择客户代码" clearable style="width: 200px;" class="filter-item" />
      <el-input v-model="listQuery.name" placeholder="请选择客户名称" clearable style="width: 200px;" class="filter-item" />
      <el-select v-model="listQuery.status" placeholder="请选择状态" clearable class="filter-item">
        <el-option key="0" label="停用" value="0" />
        <el-option key="1" label="启用" value="1" />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :loading="listLoading"
        @click="handleFilter()"
      >
        {{ listLoading?'加载中':'查找' }}
      </el-button>
      <el-button
        v-waves
        plain
        class="filter-item"
        type="danger"
        :disabled="listLoading"
        @click="clearSearch()"
      >
        清空
      </el-button>
    </div>
    <div class="handle-wrapper">
      <div style="float: right">
        <el-button type="success" @click="showForm()">添加</el-button>
      </div>
    </div>
    <div style="width: 100%; height: 50px">
      <div style="float: right">
        <pagination
          v-show="totalNum>0"
          :total="totalNum"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.limit"
          @pagination="getList"
        />
      </div>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        label="ID"
        prop="id"
        width="100px"
        align="center"
      />
      <el-table-column
        label="客户代码"
        align="center"
        prop="code"
      />
      <el-table-column
        label="客户名称"
        align="center"
        prop="name"
      />
      <el-table-column
        label="状态"
        align="center"
      >
        <template slot-scope="scope">
          <span>
            <el-tag v-if="scope.row.status==0" type="danger">停用</el-tag>
            <el-tag v-if="scope.row.status==1" type="success">启用</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.add_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="primary" plain size="mini" @click="showForm(scope.row.id)">编辑</el-button>
          <el-button type="danger" plain size="mini" @click="confirmDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="totalNum>0"
      :total="totalNum"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog
      :title="formData.id?'编辑客户档案':'添加客户档案'"
      :visible.sync="dialogFormVisible"
      center
    >
      <el-form ref="formData" label-position="left" label-width="90px" :model="formData" :rules="formDataRules">
        <el-form-item label="客户代码" prop="code">
          <el-input v-model="formData.code" style="width: 90%" />
        </el-form-item>
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="formData.name" style="width: 90%" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-model="formData.status" label="0">停用</el-radio>
          <el-radio v-model="formData.status" label="1">启用</el-radio>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveData()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'ModelIndex',
  components: { Pagination },
  directives: { waves },
  data() {
    const validateName = (rule, value, callback) => {
      if (value.trim().length === 0) {
        callback(new Error('客户名称不能为空！'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.trim().length === 0) {
        callback(new Error('客户代码不能为空！'))
      } else {
        callback()
      }
    }
    return {
      tableKey: 0,
      list: [],
      totalNum: 0,
      listLoading: true,
      listQuery: {
        name: '',
        code: '',
        status: '',
        page: 1,
        limit: 20
      },
      formDataRules: {
        name: [{ required: true, trigger: 'blur', validator: validateName }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }]
      },
      formData: {
        id: '',
        code: '',
        name: '',
        status: '1'
      },
      dialogFormVisible: false
    }
  },
  created() {
    this.getList(true)
  },
  methods: {
    saveData() {
      this.$refs.formData.validate(valid => {
        if (valid) {
          let url, method, params
          if (this.formData.id) {
            url = '/admin/customer/' + this.formData.id
            method = 'put'
            params = this.formData
          } else {
            url = '/admin/customer'
            method = 'post'
            params = this.formData
          }
          params.code = params.code.trim()
          params.name = params.name.trim()
          this.$request({
            url: url,
            method: method,
            params: params
          }).then(() => {
            this.$message({
              message: '保存成功！',
              type: 'success'
            })
            this.dialogFormVisible = false
            this.getList()
          })
        }
      })
    },
    showForm(id) {
      if (!id) {
        this.formData = {
          id: '',
          code: '',
          name: '',
          status: '1'
        }
        this.dialogFormVisible = true
      } else {
        this.$request({
          url: '/admin/customer/' + id,
          method: 'get'
        }).then((response) => {
          this.formData = response.data
          this.formData.status = response.data.status.toString()
          this.dialogFormVisible = true
        }).catch(() => {
          this.listLoading = false
        })
      }
    },
    confirmDelete(id) {
      this.$confirm('是否确定删除当前记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delete(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delete(id) {
      this.$request({
        url: '/admin/customer/' + id,
        method: 'delete'
      }).then((response) => {
        if (response.code === 200) {
          this.$message({
            message: '删除成功！',
            type: 'warning'
          })
          this.handleFilter()
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    edit(id) {

    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList(true)
    },
    clearSearch() {
      this.listQuery.code = ''
      this.listQuery.name = ''
      this.listQuery.status = ''
      this.listQuery.page = ''
      this.handleFilter()
    },
    getList(showRecord = false) {
      this.listLoading = true
      const params = this.listQuery
      this.$request({
        url: '/admin/customer',
        method: 'get',
        params: params
      }).then((response) => {
        this.list = response.data.list
        this.totalNum = response.data.totalNum
        if (showRecord === true) {
          this.$notify({
            title: '搜索成功',
            message: '已经为您搜索到条' + this.totalNum + '数据！',
            type: 'success'
          })
        }
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    }
  }
}
</script>
<style scoped>
  .handle-wrapper {
    width: 100%;
    height: 50px;
    line-height: 45px;
    border-top: 1px #DCDCDC solid;
    border-bottom: 1px dashed #DCDCDC
  }
</style>
